﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IECaptAgent
{
    static class Program
    {
        /// <summary>
        /// 해당 응용 프로그램의 주 진입점입니다.
        /// </summary>
        [STAThread]
        static void Main()
        {
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());

            string splitStravaValue = string.Empty;

            try
            {
                splitStravaValue   = (string)IECaptAgent.Properties.Settings.Default["splitStravaValue"];

                string[]    arStravaValue   = splitStravaValue.Split(' ');
                string      stravaURL       = string.Empty;
                int         ValueLoopCount        = 0;

                for (ValueLoopCount = 0; ValueLoopCount < arStravaValue.Length; ValueLoopCount++)
                {
                    string arStravaUser = arStravaValue[ValueLoopCount].Split('/')[0];
                    string arStravaKey  = arStravaValue[ValueLoopCount].Split('/')[1];
                    string stravaSummaryURL = string.Format("http://www.strava.com/athletes/{0}/activity-summary/{1}", arStravaUser, arStravaKey);
                    string stravaRidesURL = string.Format("http://www.strava.com/athletes/{0}/latest-rides/{1}", arStravaUser, arStravaKey);

                    IECaptExcute(stravaSummaryURL, arStravaUser, "SUMMAY");
                    IECaptExcute(stravaRidesURL, arStravaUser, "RIDES");
                }

                int rendomSeek = (new Random()).Next(0, arStravaValue.Length);
                string rdStravaUser = arStravaValue[rendomSeek].Split('/')[0];
                string rdStravaKey = arStravaValue[rendomSeek].Split('/')[1];
                string rdStravaSummaryURL = string.Format("http://www.strava.com/athletes/{0}/activity-summary/{1}", rdStravaUser, rdStravaKey);
                string rdStravaRidesURL = string.Format("http://www.strava.com/athletes/{0}/latest-rides/{1}", rdStravaUser, rdStravaKey);

                IECaptExcute(rdStravaSummaryURL, "RANDOM", "SUMMAY");
                IECaptExcute(rdStravaRidesURL, "RANDOM", "RIDES");

            }
            catch (Exception ex)
            {

            }
            finally
            {

            }
        }

        /// <summary>
        /// IECapt 를 실행하여 실제 이미지 챕처
        /// </summary>
        /// <param name="stravaRidesURL">스트라바 대상 주소</param>
        /// <param name="stravaUser">저장될 파일명</param>
        /// <param name="captType">챕처타입 (SUMMAY, RIDES)</param>
        static void IECaptExcute(string stravaRidesURL, string fileName, string captType)
        {
            Process process = new Process();

            process.StartInfo.FileName = System.IO.Path.Combine(Application.StartupPath, "IECapt.exe");
            process.StartInfo.WorkingDirectory = IECaptAgent.Properties.Settings.Default["StravaImgPath"].ToString();
            process.StartInfo.Arguments = string.Format("--url={0} --out={1}_{2}.png --min-width={3}", stravaRidesURL, captType, fileName, 300);
            process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            process.EnableRaisingEvents = true;
            process.Start();

            while (process.HasExited == false)
            {
                process.WaitForExit(100);
            }
        }
    }
}
